import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
from multiprocessing import Pool

url_home = "https://www.dytt8.net"
client = MongoClient("mongodb://localhost:27017/")
db = client["film_db"]
collection = db["film_collection"]
headers = {
	"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
}


def get_html(url):
	if '//' not in url:
		url = "http://www.ygdy8.net" + url
	print(url)
	response = requests.get(url, headers=headers)
	response.encoding = 'GB2312' #response.apparent_encoding
	return response.text


def analysis_detail(url, tab_name, film_name):
	print("开始解析url", url)
	html = get_html(url)
	soup = BeautifulSoup(html,'lxml')
	zoom = soup.find(id='Zoom')
	if zoom:
		print(zoom)
		mainImgUrl = zoom.find("img") # 主图
		print(111,mainImgUrl)
		p = zoom.find("p")
		if p:
			detail = p.text.replace("◎","\n") # 详情
			imgUrl = p.find_all("img")
		else:
			details = zoom.find_all("br")
			imgUrl = []
		if details:
			detail = ""
			for br in details:
				detail += br.text
		# imgUrl = zoom.find("p").find_all("img") # 详情图
		imgs = []
		for img in imgUrl:
			if img:
				imgs.append(img.get('src'))
		urls = []
		for table in zoom.find_all("table"):
			if table:
				for a in table.find_all("a"):
					if a:
						urls.append(a.get("href"))
		data = {
			"tab_name": tab_name,
			"film_name": film_name,
			"main_img": mainImgUrl.get("src"),
			"detail": detail,
			"imgs": imgs,
			"urls": urls
		}
		# collection.insert_one(data)
		print(data)


def analysis_tab(url, name):
	if "游戏" in name:
		return
	print("开始解析tab：\t", url)
	html = get_html(url)
	soup = BeautifulSoup(html, 'lxml')
	tables = soup.find(class_='co_content8').find_all('table')
	tables_num = 0
	for table in tables:
		trs = table.find_all('tr')
		if len(trs) == 4:
			tr = trs[1]
			url = tr.find('a')
			tables_num += 1
			if url:
				href = url.get('href')
				print(name, href)
				# analysis_detail(href, name, url.text)
		else:
			with open('logs.log', 'a', encoding='utf-8') as f:
				f.write("解析tab出错\t" + url + "\n")
	print('table_num',tables_num)


def main():
	# analysis_tab("http://www.ygdy8.net/html/gndy/china/index.html","test")
	analysis_detail("http://www.ygdy8.net/html/tv/oumeitv/20181024/57653.html", "最新电影", "特种兵归来1：血狼之怒")
	


if __name__ == '__main__':
	main()